﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.IO;

namespace Eugenhome.Atrium.Bookmark.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class BookmarkEdit : System.Web.UI.Page
    {
        /// <summary></summary>
        private Login _login = new Login();

        /// <summary></summary>
        private BookmarkCollection _bc;

        /// <summary></summary>
        public Bookmark SelectedBookmark { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _login.Check();

            _bc = new BookmarkCollection();

            _bc.DataFullFileName = Server.MapPath("bookmark.xml");
            _bc.Load();
            SelectedBookmark = _bc.BookmarkList.Where(item => item.ID.ToString().Equals(lstBookmarks.SelectedValue)).FirstOrDefault();


            if (!IsPostBack)
            {
                //Files binden
                List<BaseFile> files = new BookmarkLinkFile("").GetFiles(Server.MapPath(""));
                files.Insert(0, new BookmarkLinkFile("[Externes URL]"));

                cbLink.DataSource = files;
                cbLink.DataTextField = "DisplayName";
                cbLink.DataValueField = "HttpPath";
                cbLink.DataBind();

                //Icons binden
                List<BaseFile> icons = new BookmarkIconFile("").GetFiles(Server.MapPath(""));
                icons.Insert(0, new BookmarkLinkFile("[Externes URL]"));

                cbIcon.DataSource = icons;
                cbIcon.DataTextField = "DisplayName";
                cbIcon.DataValueField = "HttpPath";
                cbIcon.DataBind();

                //Labels binden
                string savedSelectedLabel = _bc.SelectedLabel;
                cbLabels.DataSource = _bc.LabelList;
                cbLabels.DataBind();
                cbLabels.SelectedValue = savedSelectedLabel;

                cbLabels_SelectedIndexChanged(null, null);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbLabels_SelectedIndexChanged(object sender, EventArgs e)
        {
            _bc.SetSelectedLabel(cbLabels.SelectedItem.ToString());

            lstBookmarks.DataSource = _bc.GetBookmarks();
            lstBookmarks.DataTextField = "DisplayName";
            lstBookmarks.DataValueField = "ID";
            lstBookmarks.DataBind();

            if (SelectedBookmark == null || !lstBookmarks.Items.Cast<ListItem>().Any(item => item.Value.Equals(SelectedBookmark.ID.ToString())))
            {
                if (!lstBookmarks.Items.Count.Equals(0))
                {
                    lstBookmarks.SelectedValue = lstBookmarks.Items[0].Value;
                    lstBookmarks_SelectedIndexChanged(null, null);
                }
            }
            else
            {
                lstBookmarks.SelectedValue = SelectedBookmark.ID.ToString();
                lstBookmarks_SelectedIndexChanged(null, null);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lstBookmarks_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedBookmark = _bc.BookmarkList.Where(item => item.ID.ToString().Equals(lstBookmarks.SelectedValue)).FirstOrDefault();

            #region LINK

            try
            {
                if (SelectedBookmark.Crypted)
                {
                    if (cbLink.Items.Cast<ListItem>().Any(item => item.Value.Equals(SelectedBookmark.Href)))
                    {
                        cbLink.SelectedValue = SelectedBookmark.Href;
                    }
                    else
                    {
                        cbLink.SelectedIndex = 0;
                        txtHref.Text = "";
                    }
                }
                else
                {
                    cbLink.SelectedIndex = 0;
                }
            }
            catch
            {
                if (!cbLabels.Items.Count.Equals(0))
                {
                    cbLabels.SelectedIndex = 0;
                }
            }

            cbLink_SelectedIndexChanged(null, null);

            #endregion link

            #region ICON

            try
            {
                if (cbIcon.Items.Cast<ListItem>().Any(item => item.Value.Equals(SelectedBookmark.Icon)))
                {
                    cbIcon.SelectedValue = SelectedBookmark.Icon;
                }
                else
                {
                    cbIcon.SelectedIndex = 0;
                    txtIcon.Text = "";
                }
            }
            catch
            {
                if (!cbIcon.Items.Count.Equals(0))
                {
                    cbIcon.SelectedIndex = 0;
                }
            }

            cbIcon_SelectedIndexChanged(null, null);

            #endregion icon

            this.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            string savedSelectedLabel = _bc.SelectedLabel;

            SelectedBookmark.DisplayName = txtDisplayName.Text;
            SelectedBookmark.Href = txtHref.Text;
            SelectedBookmark.LabelsString = txtLabels.Text;
            SelectedBookmark.OpenInNewWindow = cbNewWindow.Checked;
            SelectedBookmark.Icon = txtIcon.Text;
            SelectedBookmark.Crypted = !cbLink.SelectedIndex.Equals(0);

            SelectedBookmark.Update();

            UpdateBookmarkList();
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateBookmarkList()
        {
            cbLabels.DataSource = _bc.LabelList;
            cbLabels.DataBind();
            try
            {
                cbLabels.SelectedValue = _bc.SelectedLabel;
            }
            catch
            {
                if (!cbLabels.Items.Count.Equals(0))
                {
                    cbLabels.SelectedIndex = 0;
                }
            }

            cbLabels_SelectedIndexChanged(null, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            SelectedBookmark = _bc.BookmarkList.Where(item => item.ID.ToString().Equals(lstBookmarks.SelectedValue)).FirstOrDefault();

            if (SelectedBookmark != null)
            {
                SelectedBookmark.Delete();

                if (!lstBookmarks.Items.Count.Equals(0))
                {
                    UpdateBookmarkList();

                    lstBookmarks.SelectedIndex = 0;

                    lstBookmarks_SelectedIndexChanged(null, null);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            SelectedBookmark = new Bookmark();
            SelectedBookmark.DisplayName = "";
            SelectedBookmark.Href = "";
            SelectedBookmark.Icon = "";
            SelectedBookmark.LabelsString = "• Alle;" + _bc.SelectedLabel;

            _bc.Add(SelectedBookmark);

            SelectedBookmark.Update();

            this.DataBind();

            UpdateBookmarkList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbLink_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbLink.SelectedIndex.Equals(0))
            {
                SelectedBookmark.Crypted = false;
                txtHref.Text = SelectedBookmark != null ? SelectedBookmark.Href : "";
                txtHref.Enabled = true;
            }
            else
            {
                SelectedBookmark.Crypted = true;
                txtHref.Text = cbLink.SelectedValue;
                txtHref.Enabled = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbIcon_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbIcon.SelectedIndex.Equals(0))
            {
                txtIcon.Text = SelectedBookmark != null ? SelectedBookmark.Href : "";
                txtIcon.Enabled = true;
            }
            else
            {
                txtIcon.Text = cbIcon.SelectedValue;
                txtIcon.Enabled = false;
            }
        }

                /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMoveDown_Click(object sender, ImageClickEventArgs e)
        {
            SelectedBookmark = _bc.BookmarkList.Where(item => item.ID.ToString().Equals(lstBookmarks.SelectedValue)).FirstOrDefault();

            ++SelectedBookmark.Sort;

            SelectedBookmark.Update();
            _bc.Load();

            UpdateBookmarkList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMoveUp_Click(object sender, ImageClickEventArgs e)
        {
            --SelectedBookmark.Sort;

            SelectedBookmark.Update();

            UpdateBookmarkList();
        }

    }
}